$(function () {
    $('body').app({
        onTaskBlankContextMenu:onTaskBlankContextMenu,
        onAppContextMenu:onAppContextMenu,
        onWallContextMenu:onWallContextMenu,
		onStartMenuClick:onStartMenuClick
    });
	
	function onStartMenuClick(item){
		var data = $(item.target).data("data");
		$('body').app("createwindow",data);
	}

    var appListMenuData = [
        {
            "text":"关闭"
        },
        {
            "text":"关闭其他"
        },
        {
            "text":"关闭所有"
        },
        {
            "text":"任务管理器"
        },
        {
            "text":"属性"
        }
    ];

    var appListMenu = $('body').app('createMenu', {data:appListMenuData,opt:{onClick:onTaskBlankContextMenuClick}});

    function onTaskBlankContextMenu(e, appid) {
        if (appid) {
            console.log(appid);
            appListMenu.menu('findItem', '任务管理器').target.style.display = 'none';
            appListMenu.menu('findItem', '属性').target.style.display = 'none';
            appListMenu.menu('findItem', '关闭').target.style.display = 'block';
            appListMenu.menu('findItem', '关闭其他').target.style.display = 'block';
            appListMenu.menu('findItem', '关闭所有').target.style.display = 'block';
        } else {
            appListMenu.menu('findItem', '任务管理器').target.style.display = 'block';
            appListMenu.menu('findItem', '属性').target.style.display = 'block';
            appListMenu.menu('findItem', '关闭').target.style.display = 'none';
            appListMenu.menu('findItem', '关闭其他').target.style.display = 'none';
            appListMenu.menu('findItem', '关闭所有').target.style.display = 'none';
        }

        appListMenu.menu('show', {
            left:e.pageX,
            top:e.pageY,
            onHide:function(){
            	appListMenu.menu('show',{onHide:function(){}});
            }
        });
		APPID = appid;
        e.preventDefault();
    }
    
    //任务栏右击关闭事件
	function onTaskBlankContextMenuClick(item){
		if(item.text == '关闭'){
			$('body').app("closeapp",APPID);
		}
		if(item.text == '关闭其他'){
			var opened = $('div[w_id]').length;
			if(opened <= 1){return;}//只有一项
			$.each($('div[w_id]'), function(index, item){
				var uuid = $(item).attr('w_id');
				if(uuid != APPID){
					$('body').app("closeapp",uuid);
				}
			})
		}
		if(item.text == '关闭所有'){
			$.each($('div[w_id]'), function(index, item){
				var uuid = $(item).attr('w_id');
				$('body').app("closeapp",uuid);
			})
		}

    	appListMenu.menu('hide');
	}
	

    var wallMenuData = [
        {
            "text":"属性",
            "href":"http://www.baidu.com"
        },
        '-',
        {
            "text":"关于",
            "href":"http://www.baidu.com"
        }
    ];
    var appMenuData = [
        {
            "text":"打开"
        },
        '-',
        {
            "text":"删除"
        },
        '-',
        {
            "text":"属性"
        }
    ];

    var wallMenu = $('body').app('createMenu', {data:wallMenuData,opt:{onClick:onStartMenuClick}});
    var appMenu = $('body').app('createMenu', {data:appMenuData,opt:{onClick:onAppContextMenuClick}});

	var APPID;
    function onAppContextMenu(e,appid) {
        appMenu.menu('show', {
            left:e.pageX,
            top:e.pageY,
            onHide:function(){
            	appMenu.menu('show',{onHide:function(){}});
            }
        });
		APPID = appid;
    }
	
	function onAppContextMenuClick(item){
		var appItem = $("li[app_id='"+APPID+"']");
		if(item.text == '打开'){
			appItem.dblclick();
		}
		if(item.text == '属性'){
			alert('提供换图标功能，敬请期待');
		}
		if(item.text == '删除'){
			var _desktopItemId = appItem.attr('id');
			if(_desktopItemId && _desktopItemId>0){
				_removeMenu2Desktop(_desktopItemId);
			}else{
				showSuccessToast('showSuccessToast', "内置功能，无法删除！");
			}
		}
	}
	

	function _removeMenu2Desktop(_desktopItemId){
		var data = {
			'desktopItemId':_desktopItemId
		}
		$.ajax({
			type:"POST",
			url:'desktopAjax_removeMenu2Desktop.action',
			dataType:"json",
			data:data,
			success:function(msg){
				showSuccessToast('showSuccessToast', "删除成功！");
				refreshapp();
			},
			error:function(er){
				alert(er.responseText.trim());
			}
		});
	}
	

    function onWallContextMenu(e) {
        wallMenu.menu('show', {
            left:e.pageX,
            top:e.pageY,
            onHide:function(){
            	wallMenu.menu('show',{onHide:function(){}});
            }
        });
    }
});
